package client

import (
    "net/http"
    "net/url"
    "testing"

    "fcs-api/src/api"
    "git.inspur.com/x/clustermanager-client/cluster"
    "github.com/emicklei/go-restful"
)

const clusterManagerHost = "http://10.110.25.114:30486"

func TestNewClusterClientManager(t *testing.T) {
    clusterManager, err := cluster.New(clusterManagerHost)
    if err != nil {
        t.Errorf("new cluster client manager error. %v", err)
    }
    NewClusterClientManager(clusterManager)
}

func TestClusterClient(t *testing.T) {
    requestUrl, err := url.Parse("http://localhost:9090/apis/core/v1/envs/Cluster77/namespaces")
    cases := []struct {
        request *restful.Request
    }{
        {
            &restful.Request{
                Request: &http.Request{
                    Header: http.Header(map[string][]string{}),
                    URL:    requestUrl,
                },
            },
        },
    }
    clusterManager, err := cluster.New(clusterManagerHost)
    if err != nil {
        t.Errorf("new cluster client manager error. %v", err)
    }
    manager := NewClusterClientManager(clusterManager)

    if err != nil {
        t.Errorf("new cluster client manager error. %v", err)
    }
    for _, c := range cases {
        client, err := manager.Client(c.request)
        if err != nil {
            t.Fatalf("Client(%v): Expected client to be created but error was thrown:"+
                " %s", c.request, err.Error())
        }
        nss, err := client.CoreV1().Namespaces().List(api.ListEverything)
        if err != nil {
            t.Fatalf("client %v get namespaces error was thrown: %s", client, err.Error())
        }
        for _, ns := range nss.Items {
            println(ns.Name)
        }
    }
}
